package org.qloudgen.borg.messaging;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hornetq.api.core.TransportConfiguration;  
import org.hornetq.core.config.Configuration;
import org.hornetq.core.config.impl.ConfigurationImpl;
import org.hornetq.core.remoting.impl.invm.InVMAcceptorFactory;
import org.hornetq.core.server.HornetQServers;
import org.hornetq.jms.server.JMSServerManager;
import org.hornetq.jms.server.impl.JMSServerManagerImpl;

/*
 * start Hornetq server and create queue 
 */
public class HornetqJmsServer  
{  
	static Log log = LogFactory.getLog(HornetqJmsServer.class);
	private String queueName;
	
	public HornetqJmsServer(String queueName){
		this.setQueueName(queueName);
	}
	
	public void start() 
	   {
		  JMSServerManager jmsServerManager = null;
	      try
	      {
	         // Step 1. Create the Configuration, and set the properties accordingly
	         Configuration configuration = new ConfigurationImpl();
	         configuration.setPersistenceEnabled(false);
	         configuration.setSecurityEnabled(false);
	         configuration.getAcceptorConfigurations().add(new TransportConfiguration(InVMAcceptorFactory.class.getName()));

	         // Step 2. Create and start the jmsServer
	         jmsServerManager = new JMSServerManagerImpl(HornetQServers.newHornetQServer(configuration), "hornetq-jms.xml");  
	         jmsServerManager.setContext(null);  
	         jmsServerManager.start(); 
	         
	         String[] jndi = {this.getQueueName()};
	         
	         jmsServerManager.createQueue(true, this.getQueueName(), null, true, jndi);
	       
	      }catch (Exception e)
	      {
	         log.error(e.getMessage());
	         
	      } 
	      
	   }

	public String getQueueName() {
		return queueName;
	}

	public void setQueueName(String queueName) {
		this.queueName = queueName;
	}
}

